﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Eaf.Net.Core.DataAccess;
using Eaf.Net.AccessControl.DataAccess;
using Eaf.Net.AccessControl.Entity.User;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations.Schema;
using Hfm.Common.Entity.Country;
using Hfm.Common.Entity.Item;
using Hfm.Common.Entity.Warehouse;
using Hfm.Common.Entity.CustomerStock;
using Hfm.Common.Entity.Customer;
using Hfm.Common.Entity.Contract;
using Hfm.Common.Entity.Contacts;
using Hfm.Common.Entity.Employee;
using Hfm.Common.Entity.Industry;
using Hfm.Common.Entity.Parameters;

namespace Hfm.Core.DataAccess
{
    public class CoreContext : AccessControlContext<UserAccountEntity>
    {
        private const string TableCountry = "Country";
        private const string TableItem = "Item";
        private const string TableWarehouse = "Warehouse";
        private const string TableCustomer = "Customer";
        private const string TableCustomerStock = "CustomerStock";
        private const string TableCustomerStockInventory = "CustomerStockInventory";
        private const string TableCustomerStockSerialNumber = "CustomerStockSerialNumber";
        private const string TableCustomerStockEmailRecipient = "CustomerStockEmailRecipient";
        private const string TableContract = "Contract";
        private const string TableContacts = "Contacts";
        private const string TableEmployee = "Employee";
        private const string TableIndustry = "Industry";
        private const string TableParameters = "Parameters";

        public DbSet<CountryEntity> CountryEntities { get; set; }
        public DbSet<ItemEntity> ItemEntities { get; set; }
        public DbSet<WarehousEntity> WareHousEntities { get; set; }
        public DbSet<ContractEntity> ContractEntityss { get; set; }
        public DbSet<CustomerEntity> CustomerEntities { get; set; }
        public DbSet<CustomerStockEntity> CustomerStockEntities { get; set; }
        public DbSet<CustomerStockInventoryEntity> CustomerStockInventoryEntities { get; set; }
        public DbSet<CustomerStockSerialNumberEntity> CustomerStockSercialNumberEntities { get; set; }
        public DbSet<CustomerStockEmailRecipientEntity> CustomerStockEmailRecipientEntities { get; set; }
        public DbSet<ContactsEntity> ContactsEntities { get; set; }
        public DbSet<EmployeeEntity> EmployeeEntities { get; set; }
        public DbSet<IndustryEntity> IndustryEntities { get; set; }
        public DbSet<ParametersEntity> ParametersEntityes { get; set; }

        protected override void RegisterModelMapping(DbModelBuilder modelBuilder)
        {
            base.RegisterModelMapping(modelBuilder);

            #region TableCountry

            modelBuilder.Entity<CountryEntity>().ToTable(TableCountry);
            modelBuilder.Entity<CountryEntity>().HasKey(p => p.CountryId);
            modelBuilder.Entity<CountryEntity>().Property(p => p.CountryId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
            modelBuilder.Entity<CountryEntity>().Property(p => p.CountryName).IsRequired();

            #endregion

            #region Customer

            modelBuilder.Entity<CustomerEntity>().ToTable(TableCustomer);
            modelBuilder.Entity<CustomerEntity>().HasKey(p => p.Id);
            modelBuilder.Entity<CustomerEntity>().Property(p => p.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
            modelBuilder.Entity<CustomerEntity>().Property(p => p.CustomerName).IsRequired();
            modelBuilder.Entity<CustomerEntity>().Property(p => p.CustomerCode).IsRequired();
            modelBuilder.Entity<CustomerEntity>().Property(p => p.Address);
            modelBuilder.Entity<CustomerEntity>().Property(p => p.CountryId);
            modelBuilder.Entity<CustomerEntity>().Property(p => p.Email);
            modelBuilder.Entity<CustomerEntity>().Property(p => p.Employee);
            modelBuilder.Entity<CustomerEntity>().Property(p => p.FaxNumber);
            modelBuilder.Entity<CustomerEntity>().Property(p => p.IndustryId);
            modelBuilder.Entity<CustomerEntity>().Property(p => p.PostalCode);
            modelBuilder.Entity<CustomerEntity>().Property(p => p.Remarks);
            modelBuilder.Entity<CustomerEntity>().Property(p => p.Status);
            modelBuilder.Entity<CustomerEntity>().Property(p => p.TelNumber);
            modelBuilder.Entity<CustomerEntity>().Property(p => p.Website);

            #endregion

            #region TableItem

            modelBuilder.Entity<ItemEntity>().ToTable(TableItem);
            modelBuilder.Entity<ItemEntity>().HasKey(p => p.ItemId);
            modelBuilder.Entity<ItemEntity>().Property(p => p.ItemId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
            modelBuilder.Entity<ItemEntity>().Property(p => p.ItemName).IsRequired();
            modelBuilder.Entity<ItemEntity>().Property(p => p.ItemCode).IsRequired();
            modelBuilder.Entity<ItemEntity>().Property(p => p.Country).IsRequired();
            modelBuilder.Entity<ItemEntity>().Property(p => p.ItemDescription).IsRequired();
            modelBuilder.Entity<ItemEntity>().Property(p => p.ItemType).IsRequired();
            modelBuilder.Entity<ItemEntity>().Property(p => p.Manufacturer).IsRequired();
            modelBuilder.Entity<ItemEntity>().Property(p => p.Model).IsRequired();
            modelBuilder.Entity<ItemEntity>().Property(p => p.PartNumber).IsRequired();
            modelBuilder.Entity<ItemEntity>().Property(p => p.Price).IsRequired();
            modelBuilder.Entity<ItemEntity>().Property(p => p.Remarks).IsRequired();
            modelBuilder.Entity<ItemEntity>().Property(p => p.SupplierInformation).IsRequired();
            modelBuilder.Entity<ItemEntity>().Property(p => p.TrackSerialNumber).IsRequired();
            modelBuilder.Entity<ItemEntity>().Property(p => p.TrackStock).IsRequired();

            #endregion

            #region TableWarehouse

            modelBuilder.Entity<WarehousEntity>().ToTable(TableWarehouse);
            modelBuilder.Entity<WarehousEntity>().HasKey(p => p.WarehouseId);
            modelBuilder.Entity<WarehousEntity>().Property(p => p.WarehouseId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
            modelBuilder.Entity<WarehousEntity>().Property(p => p.WarehouseName).IsRequired();
            modelBuilder.Entity<WarehousEntity>().Property(p => p.WarehouseAddress).IsRequired();
            modelBuilder.Entity<WarehousEntity>().Property(p => p.Remarks).IsRequired();
            modelBuilder.Entity<WarehousEntity>().Property(p => p.PostalCode).IsRequired();
            modelBuilder.Entity<WarehousEntity>().Property(p => p.Country).IsRequired();

            #endregion

            #region CustomerStock

            modelBuilder.Entity<CustomerStockEntity>().ToTable(TableCustomerStock);
            modelBuilder.Entity<CustomerStockEntity>().HasKey(p => p.Id);
            modelBuilder.Entity<CustomerStockEntity>().Property(p => p.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
            modelBuilder.Entity<CustomerStockEntity>().Property(p => p.CustomerId).IsRequired();
            modelBuilder.Entity<CustomerStockEntity>().Property(p => p.ItemId).IsRequired();
            modelBuilder.Entity<CustomerStockEntity>().Property(p => p.ThresholdQuantity).IsRequired();

            #endregion

            #region CustomerStockInventory

            modelBuilder.Entity<CustomerStockInventoryEntity>().ToTable(TableCustomerStockInventory);
            modelBuilder.Entity<CustomerStockInventoryEntity>().HasKey(p => p.Id);
            modelBuilder.Entity<CustomerStockInventoryEntity>().Property(p => p.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
            modelBuilder.Entity<CustomerStockInventoryEntity>().Property(p => p.StockId).IsRequired();
            modelBuilder.Entity<CustomerStockInventoryEntity>().Property(p => p.WarehouseId).IsRequired();
            modelBuilder.Entity<CustomerStockInventoryEntity>().Property(p => p.Quantity).IsRequired();

            #endregion

            #region CustomerStockSerialNumber

            modelBuilder.Entity<CustomerStockSerialNumberEntity>().ToTable(TableCustomerStockSerialNumber);
            modelBuilder.Entity<CustomerStockSerialNumberEntity>().HasKey(p => p.Id);
            modelBuilder.Entity<CustomerStockSerialNumberEntity>().Property(p => p.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
            modelBuilder.Entity<CustomerStockSerialNumberEntity>().Property(p => p.StockId).IsRequired();
            modelBuilder.Entity<CustomerStockSerialNumberEntity>().Property(p => p.StockInventoryId).IsRequired();
            modelBuilder.Entity<CustomerStockSerialNumberEntity>().Property(p => p.SerialNumber).IsRequired();
            modelBuilder.Entity<CustomerStockSerialNumberEntity>().Property(p => p.InStockDate).IsRequired();
            modelBuilder.Entity<CustomerStockSerialNumberEntity>().Property(p => p.WarehouseId).IsRequired();
            modelBuilder.Entity<CustomerStockSerialNumberEntity>().Property(p => p.Rack);
            modelBuilder.Entity<CustomerStockSerialNumberEntity>().Property(p => p.Pallet);
            modelBuilder.Entity<CustomerStockSerialNumberEntity>().Property(p => p.Remarks);

            #endregion

            #region CustomerStockEmailRecipient

            modelBuilder.Entity<CustomerStockEmailRecipientEntity>().ToTable(TableCustomerStockEmailRecipient);
            modelBuilder.Entity<CustomerStockEmailRecipientEntity>().HasKey(p => p.Id);
            modelBuilder.Entity<CustomerStockEmailRecipientEntity>().Property(p => p.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
            modelBuilder.Entity<CustomerStockEmailRecipientEntity>().Property(p => p.UserId).IsRequired();
            modelBuilder.Entity<CustomerStockEmailRecipientEntity>().Property(p => p.StockId).IsRequired();
            modelBuilder.Entity<CustomerStockEmailRecipientEntity>().Property(p => p.Enabled).IsRequired();

            #endregion

            #region Contract

            modelBuilder.Entity<ContractEntity>().ToTable(TableContract);
            modelBuilder.Entity<ContractEntity>().HasKey(p => p.ID);
            modelBuilder.Entity<ContractEntity>().Property(p => p.ID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
            modelBuilder.Entity<ContractEntity>().Property(p => p.AcceptedDate);
            modelBuilder.Entity<ContractEntity>().Property(p => p.ContractName).IsRequired();
            modelBuilder.Entity<ContractEntity>().Property(p => p.ContractNumber).IsRequired();
            modelBuilder.Entity<ContractEntity>().Property(p => p.ContractPeriod).IsRequired();
            modelBuilder.Entity<ContractEntity>().Property(p => p.CustomerID);
            modelBuilder.Entity<ContractEntity>().Property(p => p.Description);
            modelBuilder.Entity<ContractEntity>().Property(p => p.Remarks);
            modelBuilder.Entity<ContractEntity>().Property(p => p.StartData);
            modelBuilder.Entity<ContractEntity>().Property(p => p.Status);
            modelBuilder.Entity<ContractEntity>().Property(p => p.TermsAndConditions);

            #endregion

            #region Contacts

            modelBuilder.Entity<ContactsEntity>().ToTable(TableContacts);
            modelBuilder.Entity<ContactsEntity>().HasKey(p => p.ID);
            modelBuilder.Entity<ContactsEntity>().Property(p => p.ID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
            modelBuilder.Entity<ContactsEntity>().Property(p => p.FullName).IsRequired();
            modelBuilder.Entity<ContactsEntity>().Property(p => p.OfficeNumber).IsRequired();
            modelBuilder.Entity<ContactsEntity>().Property(p => p.Mobile).IsRequired();
            modelBuilder.Entity<ContactsEntity>().Property(p => p.FaxNumber).IsRequired();
            modelBuilder.Entity<ContactsEntity>().Property(p => p.Email).IsRequired();
            modelBuilder.Entity<ContactsEntity>().Property(p => p.Designation);
            modelBuilder.Entity<ContactsEntity>().Property(p => p.SalutationID);
            modelBuilder.Entity<ContactsEntity>().Property(p => p.Department);
            modelBuilder.Entity<ContactsEntity>().Property(p => p.Remarks);
            modelBuilder.Entity<ContactsEntity>().Property(p => p.CID).IsRequired();

            #endregion

            #region Employee

            modelBuilder.Entity<EmployeeEntity>().ToTable(TableEmployee);
            modelBuilder.Entity<EmployeeEntity>().HasKey(p => p.Id);
            modelBuilder.Entity<EmployeeEntity>().Property(p => p.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
            modelBuilder.Entity<EmployeeEntity>().Property(p => p.Name).IsRequired();

            #endregion

            #region Industry

            modelBuilder.Entity<IndustryEntity>().ToTable(TableIndustry);
            modelBuilder.Entity<IndustryEntity>().HasKey(p => p.Id);
            modelBuilder.Entity<IndustryEntity>().Property(p => p.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
            modelBuilder.Entity<IndustryEntity>().Property(p => p.Name).IsRequired();

            #endregion

            #region Parameters

            modelBuilder.Entity<ParametersEntity>().ToTable(TableParameters);
            modelBuilder.Entity<ParametersEntity>().HasKey(p => p.Id);
            modelBuilder.Entity<ParametersEntity>().Property(p => p.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
            modelBuilder.Entity<ParametersEntity>().Property(p => p.ParamName).IsRequired();
            modelBuilder.Entity<ParametersEntity>().Property(p => p.ParamValue).IsRequired();
            modelBuilder.Entity<ParametersEntity>().Property(p => p.ParamDate).IsRequired();

            #endregion

        }
    }
    
}
